home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / gxcmap.c < prev    next >
C/C++ Source or Header  |  1993-05-19  |  15KB  |  488 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gxcmap.c */
  20. /* Color mapping and conversion for Ghostscript */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gscspace.h"
  24. #include "gxfrac.h"
  25. #include "gxlum.h"
  26. #include "gxcolor.h"
  27. #include "gxdevice.h"
  28. #include "gzcolor.h"
  29. #include "gzstate.h"
  30.  
  31. /* Convert a frac to a gx_color_value. */
  32. /* This is needed because map_rgb_color still uses gx_color_value. */
  33. #define _cv_bits (sizeof(gx_color_value) * 8)
  34. #define frac2cv(fr)\
  35.   ( ((fr) << (_cv_bits - frac_bits)) +\
  36.     ((fr) >> (frac_bits * 2 - _cv_bits)) )
  37. #define cv2frac(cv) ((frac)((cv) >> (_cv_bits - frac_bits)))
  38.  
  39. /* Note: the color model conversion algorithms are taken from */
  40. /* Rogers, Procedural Elements for Computer Graphics, pp. 401-403. */
  41.  
  42. /* ------ Conversion between HSB and RGB ------ */
  43.  
  44. /* Convert RGB to HSB. */
  45. void
  46. color_rgb_to_hsb(floatp r, floatp g, floatp b, float hsb[3])
  47. {    frac red = float2frac(r), green = float2frac(g), blue = float2frac(b);
  48. #define rhue hsb[0]
  49. #define rsat hsb[1]
  50. #define rbri hsb[2]
  51.     if ( red == green && green == blue )
  52.        {    rhue = 0;    /* arbitrary */
  53.         rsat = 0;
  54.         rbri = r;    /* pick any one */
  55.        }
  56.     else
  57.        {    /* Convert rgb to hsb */
  58.         frac V, Temp;
  59.         long diff, H;
  60.         V = (red > green ? red : green);
  61.         if ( blue > V ) V = blue;
  62.         Temp = (red > green ? green : red);
  63.         if ( blue < Temp ) Temp = blue;
  64.         diff = V - Temp;
  65.         if ( V == red )
  66.             H = (green - blue) * frac_1_long / diff;
  67.         else if ( V == green )
  68.             H = (blue - red) * frac_1_long / diff + 2 * frac_1_long;
  69.         else /* V == blue */
  70.             H = (red - green) * frac_1_long / diff + 4 * frac_1_long;
  71.         if ( H < 0 ) H += 6 * frac_1_long;
  72.         rhue = H / (frac_1 * 6.0);
  73.         rsat = diff / (float)V;
  74.         rbri = frac2float(V);
  75.        }
  76. #undef rhue
  77. #undef rsat
  78. #undef rbri
  79. }
  80.  
  81. /* Convert HSB to RGB. */
  82. void
  83. color_hsb_to_rgb(floatp hue, floatp saturation, floatp brightness, float rgb[3])
  84. {    if ( saturation == 0 )
  85.        {    rgb[0] = rgb[1] = rgb[2] = brightness;
  86.        }
  87.     else
  88.        {    /* Convert hsb to rgb. */
  89.         /* We rely on the fact that the product of two */
  90.         /* fracs fits into an unsigned long. */
  91.         floatp h6 = hue * 6;
  92.         ulong V = float2frac(brightness);    /* force arithmetic to long */
  93.         frac S = float2frac(saturation);
  94.             int I = (int)h6;
  95.         ulong F = float2frac(h6 - I);        /* ditto */
  96.         /* M = V*(1-S), N = V*(1-S*F), K = V*(1-S*(1-F)) = M-N+V */
  97.         frac M = V * (frac_1_long - S) / frac_1_long;
  98.         frac N = V * (frac_1_long - S * F / frac_1_long) / frac_1_long;
  99.         frac K = M - N + V;
  100.         frac R, G, B;
  101.         switch ( I )
  102.            {
  103.         default: R = V; G = K; B = M; break;
  104.         case 1: R = N; G = V; B = M; break;
  105.         case 2: R = M; G = V; B = K; break;
  106.         case 3: R = M; G = N; B = V; break;
  107.         case 4: R = K; G = M; B = V; break;
  108.         case 5: R = V; G = M; B = N; break;
  109.            }
  110.         rgb[0] = frac2float(R);
  111.         rgb[1] = frac2float(G);
  112.         rgb[2] = frac2float(B);
  113. #ifdef DEBUG
  114. if ( debug_c('c') )
  115. {        dprintf7("[c]hsb(%g,%g,%g)->VSFI(%ld,%d,%ld,%d)->\n",
  116.              hue, saturation, brightness, V, S, F, I);
  117.         dprintf6("   RGB(%d,%d,%d)->rgb(%g,%g,%g)\n",
  118.              R, G, B, rgb[0], rgb[1], rgb[2]);
  119. }
  120. #endif
  121.        }
  122. }
  123.  
  124. /* ------ Color space conversion ------ */
  125.  
  126. /* Only 4 of the 6 conversions are implemented here; */
  127. /* the other 2 (Gray to RGB/CMYK) are trivial. */
  128. /* The CMYK to RGB algorithms specified by Adobe are, e.g., */
  129. /*    R = 1.0 - min(1.0, C + K)    */
  130. /* but we get much better results with */
  131. /*    R = (1.0 - C) * (1.0 - K)    */
  132.  
  133. /* Convert RGB to Gray. */
  134. frac
  135. color_rgb_to_gray(frac r, frac g, frac b, const gs_state *pgs)
  136. {    return (r * (unsigned long)lum_red_weight +
  137.         g * (unsigned long)lum_green_weight +
  138.         b * (unsigned long)lum_blue_weight +
  139.         (lum_all_weights / 2))
  140.         / lum_all_weights;
  141. }
  142.  
  143. /* Convert RGB to CMYK. */
  144. /* Note that this involves black generation and undercolor removal. */
  145. void
  146. color_rgb_to_cmyk(frac r, frac g, frac b, const gs_state *pgs,
  147.   frac cmyk[4])
  148. {    frac c = frac_1 - r, m = frac_1 - g, y = frac_1 - b;
  149.     frac k = (c < m ? min(c, y) : min(m, y));
  150.     floatp fk = frac2float(k);
  151.     /* The default UCR and BG functions are pretty arbitrary.... */
  152.     float fucr =
  153.         (pgs->undercolor_removal == NULL ? 0.0 :
  154.          (*pgs->undercolor_removal)(pgs, fk));
  155.     float fbg =
  156.         (pgs->black_generation == NULL ? 0.0 :
  157.          (*pgs->black_generation)(pgs, fk));
  158.     signed_frac ucr =
  159.       (fucr < -1.0 ? -frac_1 : fucr > 1.0 ? frac_1 : float2frac(fucr));
  160.     frac bg =
  161.       (fbg < 0.0 ? frac_0 : fbg > 1.0 ? frac_1 : float2frac(fbg));
  162.     cmyk[0] = (c < ucr ? frac_0 : ucr < c - frac_1 ? frac_1 : c - ucr);
  163.     cmyk[1] = (m < ucr ? frac_0 : ucr < m - frac_1 ? frac_1 : m - ucr);
  164.     cmyk[2] = (y < ucr ? frac_0 : ucr < y - frac_1 ? frac_1 : y - ucr);
  165.     cmyk[3] = bg;
  166. }
  167.  
  168. /* Convert CMYK to Gray. */
  169. frac
  170. color_cmyk_to_gray(frac c, frac m, frac y, frac k, const gs_state *pgs)
  171. {    frac not_gray = color_rgb_to_gray(c, m, y, pgs);
  172.     return (not_gray > frac_1 - k ?        /* gray + k > 1.0 */
  173.         frac_0 : frac_1 - (not_gray + k));
  174. }
  175.  
  176. /* Convert CMYK to RGB. */
  177. void
  178. color_cmyk_to_rgb(frac c, frac m, frac y, frac k, const gs_state *pgs,
  179.   frac rgb[3])
  180. {    switch ( k )
  181.     {
  182.     case frac_0:
  183.         rgb[0] = frac_1 - c;
  184.         rgb[1] = frac_1 - m;
  185.         rgb[2] = frac_1 - y;
  186.         break;
  187.     case frac_1:
  188.         rgb[0] = rgb[1] = rgb[2] = frac_0;
  189.         break;
  190.     default:
  191.     {    ulong not_k = frac_1 - k;
  192.         /* Compute not_k * (frac_1 - v) / frac_1 efficiently. */
  193.         ulong prod;
  194. #define deduct_black(v)\
  195.   (prod = (frac_1 - (v)) * not_k,\
  196.    (prod + (prod >> frac_bits) + 1) >> frac_bits)
  197.         rgb[0] = deduct_black(c);
  198.         rgb[1] = deduct_black(m);
  199.         rgb[2] = deduct_black(y);
  200.     }
  201.     }
  202. }
  203.  
  204. /* ------ Device color rendering ------ */
  205.  
  206. private cmap_proc_gray(cmap_gray_halftoned);
  207. private cmap_proc_gray(cmap_gray_direct);
  208. private cmap_proc_gray(cmap_gray_to_rgb);
  209. private cmap_proc_gray(cmap_gray_to_cmyk);
  210. private cmap_proc_rgb(cmap_rgb_halftoned);
  211. private cmap_proc_rgb(cmap_rgb_direct);
  212. private cmap_proc_rgb(cmap_rgb_to_gray);
  213. private cmap_proc_rgb(cmap_rgb_to_cmyk);
  214. private cmap_proc_cmyk(cmap_cmyk_halftoned);
  215. private cmap_proc_cmyk(cmap_cmyk_direct);
  216. private cmap_proc_cmyk(cmap_cmyk_to_gray);
  217. private cmap_proc_cmyk(cmap_cmyk_to_rgb);
  218.  
  219. private const gx_color_map_procs
  220.     cmap_gray_few =
  221.         { cmap_gray_halftoned, cmap_rgb_to_gray, cmap_cmyk_to_gray },
  222.     cmap_gray_many =
  223.         { cmap_gray_direct, cmap_rgb_to_gray, cmap_cmyk_to_gray },
  224.     cmap_rgb_few =
  225.         { cmap_gray_to_rgb, cmap_rgb_halftoned, cmap_cmyk_to_rgb },
  226.     cmap_rgb_many =
  227.         { cmap_gray_to_rgb, cmap_rgb_direct, cmap_cmyk_to_rgb },
  228.     cmap_cmyk_few =
  229.         { cmap_gray_to_cmyk, cmap_rgb_to_cmyk, cmap_cmyk_halftoned },
  230.     cmap_cmyk_many =
  231.         { cmap_gray_to_cmyk, cmap_rgb_to_cmyk, cmap_cmyk_direct };
  232.  
  233. const gx_color_map_procs *cmap_procs_default = &cmap_gray_many;
  234.  
  235. private const gx_color_map_procs _ds *cmap_few[] = {
  236.     0, &cmap_gray_few, 0, &cmap_rgb_few, &cmap_cmyk_few
  237. };
  238.  
  239. private const gx_color_map_procs _ds *cmap_many[] = {
  240.     0, &cmap_gray_many, 0, &cmap_rgb_many, &cmap_cmyk_many
  241. };
  242.  
  243. /* Set the color mapping procedures in the graphics state. */
  244. void
  245. gx_set_cmap_procs(gs_state *pgs)
  246. {    gx_device *dev = gs_currentdevice(pgs);
  247.     pgs->cmap_procs =
  248.         ((gx_device_has_color(dev) ? dev->color_info.max_rgb :
  249.           dev->color_info.max_gray) >= 31 ? cmap_many : cmap_few)
  250.          [dev->color_info.num_components];
  251. }
  252.  
  253. /* Remap the color in the graphics state. */
  254. int
  255. gx_remap_color(gs_state *pgs)
  256. {    const gs_color_space *pcs = pgs->color_space;
  257.     return (*pcs->type->remap_color)(pgs->ccolor, pcs,
  258.                 pgs->dev_color, pgs);
  259. }
  260. /* Color remappers for the standard color spaces. */
  261. #define unit_frac(v)\
  262.   (ftemp = (v),\
  263.    (ftemp < 0.0 ? frac_0 : ftemp > 1.0 ? frac_1 : float2frac(ftemp)))
  264. int
  265. gx_remap_DeviceGray(const gs_client_color *pc, const gs_color_space *pcs,
  266.   gx_device_color *pdc, gs_state *pgs)
  267. {    float ftemp;
  268.     (*pgs->cmap_procs->map_gray)
  269.         (unit_frac(pc->paint.values[0]),
  270.          pdc, pgs);
  271.     return 0;
  272. }
  273. int
  274. gx_remap_DeviceRGB(const gs_client_color *pc, const gs_color_space *pcs,
  275.   gx_device_color *pdc, gs_state *pgs)
  276. {    float ftemp;
  277.     (*pgs->cmap_procs->map_rgb)
  278.         (unit_frac(pc->paint.values[0]),
  279.          unit_frac(pc->paint.values[1]),
  280.          unit_frac(pc->paint.values[2]),
  281.          pdc, pgs);
  282.     return 0;
  283. }
  284. int
  285. gx_remap_DeviceCMYK(const gs_client_color *pc, const gs_color_space *pcs,
  286.   gx_device_color *pdc, gs_state *pgs)
  287. {    float ftemp;
  288.     (*pgs->cmap_procs->map_cmyk)
  289.         (unit_frac(pc->paint.values[0]),
  290.          unit_frac(pc->paint.values[1]),
  291.          unit_frac(pc->paint.values[2]),
  292.          unit_frac(pc->paint.values[3]),
  293.          pdc, pgs);
  294.     return 0;
  295. }
  296. #undef unit_frac
  297.  
  298. /* Render Gray color. */
  299.  
  300. private void
  301. cmap_gray_direct(frac gray, gx_device_color *pdc, const gs_state *pgs)
  302. {    gx_device *dev = gs_currentdevice(pgs);
  303.     frac mgray = gx_map_color_frac(pgs, gray, gray);
  304.     gx_color_value cv_gray = frac2cv(mgray);
  305.     gx_color_index color =
  306.         (*dev->procs->map_rgb_color)(dev, cv_gray, cv_gray, cv_gray);
  307.     if ( color == gx_no_color_index )
  308.     {    gx_render_gray(mgray, pdc, pgs);
  309.         return;
  310.     }
  311.     pdc->color1 = pdc->color2 = color;
  312.     pdc->halftone_level = 0;
  313. }
  314.  
  315. private void
  316. cmap_gray_halftoned(frac gray, gx_device_color *pdc, const gs_state *pgs)
  317. {    gx_render_gray(gx_map_color_frac(pgs, gray, gray), pdc, pgs);
  318. }
  319.  
  320. private void
  321. cmap_gray_to_rgb(frac gray, gx_device_color *pdc, const gs_state *pgs)
  322. {    (*pgs->cmap_procs->map_rgb)(gray, gray, gray, pdc, pgs);
  323. }
  324.  
  325. private void
  326. cmap_gray_to_cmyk(frac gray, gx_device_color *pdc, const gs_state *pgs)
  327. {    (*pgs->cmap_procs->map_cmyk)(frac_0, frac_0, frac_0, gray, pdc, pgs);
  328. }
  329.  
  330. /* Render RGB color. */
  331.  
  332. private void
  333. cmap_rgb_direct(frac r, frac g, frac b, gx_device_color *pdc,
  334.   const gs_state *pgs)
  335. {    if ( r == g && g == b )
  336.     {    cmap_gray_direct(r, pdc, pgs);
  337.         return;
  338.     }
  339.     {    gx_device *dev = gs_currentdevice(pgs);
  340.         frac mred = gx_map_color_frac(pgs, r, red);
  341.         gx_color_value cv_red = frac2cv(mred);
  342.         frac mgreen = gx_map_color_frac(pgs, g, green);
  343.         gx_color_value cv_green = frac2cv(mgreen);
  344.         frac mblue = gx_map_color_frac(pgs, b, blue);
  345.         gx_color_value cv_blue = frac2cv(mblue);
  346.         gx_color_index color =
  347.             (*dev->procs->map_rgb_color)(dev,
  348.                 cv_red, cv_green, cv_blue);
  349.         if ( color == gx_no_color_index )
  350.         {    gx_render_rgb(mred, mgreen, mblue, pdc, pgs);
  351.             return;
  352.         }
  353.         pdc->color1 = pdc->color2 = color;
  354.         pdc->halftone_level = 0;
  355.     }
  356. }
  357.  
  358. private void
  359. cmap_rgb_halftoned(frac r, frac g, frac b, gx_device_color *pdc,
  360.   const gs_state *pgs)
  361. {    if ( r == g && g == b )
  362.     {    cmap_gray_halftoned(r, pdc, pgs);    /* pick any one */
  363.         return;
  364.     }
  365.     gx_render_rgb(gx_map_color_frac(pgs, r, red),
  366.               gx_map_color_frac(pgs, g, green),
  367.               gx_map_color_frac(pgs, b, blue),
  368.               pdc, pgs);
  369. }
  370.  
  371. private void
  372. cmap_rgb_to_gray(frac r, frac g, frac b, gx_device_color *pdc,
  373.   const gs_state *pgs)
  374. {    (*pgs->cmap_procs->map_gray)(color_rgb_to_gray(r, g, b, pgs), pdc, pgs);
  375. }
  376.  
  377. private void
  378. cmap_rgb_to_cmyk(frac r, frac g, frac b, gx_device_color *pdc,
  379.   const gs_state *pgs)
  380. {    frac cmyk[4];
  381.     color_rgb_to_cmyk(r, g, b, pgs, cmyk);
  382.     (*pgs->cmap_procs->map_cmyk)(cmyk[0], cmyk[1], cmyk[2], cmyk[3], pdc, pgs);
  383. }
  384.  
  385. /* Render CMYK color. */
  386.  
  387. private void
  388. cmap_cmyk_direct(frac c, frac m, frac y, frac k, gx_device_color *pdc,
  389.   const gs_state *pgs)
  390. {    if ( c == m && m == y )
  391.     {    cmap_gray_direct(color_cmyk_to_gray(c, m, y, k, pgs), pdc, pgs);
  392.         return;
  393.     }
  394.     {    gx_device *dev = gs_currentdevice(pgs);
  395.         frac mcyan = gx_map_color_frac(pgs, c, red);
  396.         gx_color_value cv_cyan = frac2cv(mcyan);
  397.         frac mmagenta = gx_map_color_frac(pgs, m, green);
  398.         gx_color_value cv_magenta = frac2cv(mmagenta);
  399.         frac myellow = gx_map_color_frac(pgs, y, blue);
  400.         gx_color_value cv_yellow = frac2cv(myellow);
  401.         frac mblack = gx_map_color_frac(pgs, k, gray);
  402.         gx_color_value cv_black = frac2cv(mblack);
  403.         gx_color_index color =
  404.             (*dev->procs->map_cmyk_color)(dev,
  405.                 cv_cyan, cv_magenta, cv_yellow, cv_black);
  406.         if ( color == gx_no_color_index )
  407.         {    cmap_cmyk_halftoned(c, m, y, k, pdc, pgs);
  408.             return;
  409.         }
  410.         pdc->color1 = pdc->color2 = color;
  411.         pdc->halftone_level = 0;
  412.     }
  413. }
  414.  
  415. private void
  416. cmap_cmyk_halftoned(frac c, frac m, frac y, frac k, gx_device_color *pdc,
  417.   const gs_state *pgs)
  418. {    /* CMYK halftones are not implemented yet. */
  419.     frac rgb[3];
  420.     color_cmyk_to_rgb(c, m, y, k, pgs, rgb);
  421.     cmap_rgb_halftoned(rgb[0], rgb[1], rgb[2], pdc, pgs);
  422. }
  423.  
  424. private void
  425. cmap_cmyk_to_gray(frac c, frac m, frac y, frac k, gx_device_color *pdc, const gs_state *pgs)
  426. {    (*pgs->cmap_procs->map_gray)(color_cmyk_to_gray(c, m, y, k, pgs), pdc, pgs);
  427. }
  428.  
  429. private void
  430. cmap_cmyk_to_rgb(frac c, frac m, frac y, frac k, gx_device_color *pdc, const gs_state *pgs)
  431. {    frac rgb[3];
  432.     color_cmyk_to_rgb(c, m, y, k, pgs, rgb);
  433.     (*pgs->cmap_procs->map_rgb)(rgb[0], rgb[1], rgb[2], pdc, pgs);
  434. }
  435.  
  436. /* ------ Transfer function mapping ------ */
  437.  
  438. /* Map a color fraction through a transfer map. */
  439. frac
  440. gx_color_frac_map(frac cv, const frac *values)
  441. {
  442. #define cp_frac_bits (frac_bits - log2_transfer_map_size)
  443.     int cmi = cv >> cp_frac_bits;
  444.     frac mv = values[cmi];
  445.     int rem, mdv;
  446.     /* Interpolate between two adjacent values if needed. */
  447.     rem = (cv & ((1 << cp_frac_bits) - 1)) - (cv >> (frac_bits - cp_frac_bits));
  448.     if ( rem == 0 ) return mv;
  449.     else if ( rem > 0 ) mdv = values[cmi + 1] - mv;
  450.     else mdv = mv - values[cmi - 1];
  451. #if arch_ints_are_short
  452.     /* Only use long multiplication if necessary. */
  453.     if ( mdv > 1 << (16 - cp_frac_bits) )
  454.         return mv + (uint)(((ulong)rem * mdv) >> cp_frac_bits);
  455. #endif
  456.     return mv + ((rem * mdv) >> cp_frac_bits);
  457. #undef cp_frac_bits
  458. }
  459.  
  460. /* ------ Default device color mapping ------ */
  461.  
  462. gx_color_index
  463. gx_default_map_rgb_color(gx_device *dev,
  464.   gx_color_value r, gx_color_value g, gx_color_value b)
  465. {    /* Map values >= 1/2 to 1, < 1/2 to 0. */
  466.     return ((r | g | b) > gx_max_color_value / 2 ?
  467.         (gx_color_index)1 : (gx_color_index)0);
  468. }
  469.  
  470. int
  471. gx_default_map_color_rgb(gx_device *dev, gx_color_index color,
  472.   gx_color_value prgb[3])
  473. {    /* Map 1 to max_value, 0 to 0. */
  474.     prgb[0] = prgb[1] = prgb[2] = -(gx_color_value)color;
  475.     return 0;
  476. }
  477.  
  478. gx_color_index
  479. gx_default_map_cmyk_color(gx_device *dev,
  480.   gx_color_value c, gx_color_value m, gx_color_value y, gx_color_value k)
  481. {    /* Convert to RGB */
  482.     frac rgb[3];
  483.     color_cmyk_to_rgb(cv2frac(c), cv2frac(m), cv2frac(y), cv2frac(k),
  484.               NULL, rgb);
  485.     return (*dev->procs->map_rgb_color)(dev,
  486.         frac2cv(rgb[0]), frac2cv(rgb[1]), frac2cv(rgb[2]));
  487. }
  488.